<template>
  <el-dialog title="员工信息" :visible.sync="visible" v-if="visible" width="80%" append-to-body v-dialogDrag :close-on-click-modal="false">
    <div class="app-container">
      <el-row :gutter="20">
        <!--用户数据-->
        <el-col :span="24" :xs="24">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
            <el-form-item label="姓名" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入"
                clearable
                size="small"
                style="width:200px"
              />
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-select v-model="queryParams.sex"
                         style="width: 2.3rem"
                         placeholder="请选择" clearable>
                <el-option
                  v-for="item in sexList"
                  :key="item.label"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
          </el-row>

          <el-table ref="multipleTable"
                    v-loading="loading"
                    :data="infoList"
                    :row-key="getRowKey"
                    @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="获取岗位渠道" align="center" prop="channel" sortable/>
            <el-table-column label="姓名" align="center" prop="name" sortable/>
            <el-table-column label="性别" align="center" prop="sex" sortable/>
            <el-table-column label="婚育状况" align="center" prop="maritalStatus" sortable/>
            <el-table-column label="政治面貌" align="center" prop="politicalOutlook" sortable/>
            <el-table-column label="手机号码" align="center" prop="phone" sortable/>
            <el-table-column label="邮箱" align="center" prop="email" sortable/>
            <el-table-column label="创建时间" align="center" prop="createTime" sortable/>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
    </div>
    <!-- 选中的用户 -->
    <div>
      <el-tag v-for="user in chooseUserList" :key="user.userName" style="margin:2px"
              closable @close="handleCloseTag(user)" >{{user.name}} </el-tag>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="primary">确认</el-button>
      <el-button @click="visible=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getWorkflowUserListByPage } from "@/api/workflow/workflowUser";
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/oa/employeeInfo";
import { deptTreeSelect } from "@/api/system/user";

export default {
  props: {
    //是否多选
    multiple: {
      type: Boolean,
      default: false,
    },
    //回显的数据
    propUser: {
      type: String,
    }
  },
  name: "employee",
  dicts: ['sys_user_sex'],
  components: {  },
  data() {
    return {
      infoList:[],
      // 遮罩层
      loading: true,
      buttonLoading: false,
      visible: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 日期范围
      dateRange: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        deptId: undefined,
        type: undefined,
        params: undefined,
        ids:[]
      },
      // 列信息
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `用户昵称`, visible: true },
        { key: 3, label: `部门`, visible: true },
        { key: 4, label: `手机号码`, visible: true },
        { key: 5, label: `创建时间`, visible: true }
      ],
      getRowKey(row) {
        return row.id;
      },
      // 保存选择的用户
      chooseUserList: [],
      flag: false,
      sexList:[
        {
          label:"男",
          value:"男",
        },{
          label:"女",
          value:"女",
        }
      ],
    };
  },
  watch: {
    visible(val){
      if (val){
        if (this.propUser){
          this.flag = true
          this.getList()
        }else{
          this.chooseUserList = []
        }
      }
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        if (this.flag &&this.infoList){

          // const result = this.controls.filter(item => item.action === action)
          this.chooseUserList=this.infoList.filter(d=>d.id==this.propUser)
          this.$nextTick(() => {
            this.chooseUserList.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row,true);
            })
            // this.$refs.multipleTable.toggleRowSelection(this.chooseUserList);
          });
          // this.chooseUserList.forEach(row => {
          //   this.$refs.multipleTable.toggleRowSelection(row,true);
          // })
        }
          //反选
          // if(this.flag && response.rows){
          //   this.chooseUserList = response.rows
          //   response.rows.forEach(row => {
          //     this.$refs.multipleTable.toggleRowSelection(row,true);
          //   })
          // }
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.flag = false
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(val) {
      if(this.multiple){
        this.chooseUserList = val.filter((element,index,self)=>{
          return self.findIndex(x=>x.userId===element.userId) === index
        })
      }else{
        this.chooseUserList = val
        if (val.length > 1) {
          let delRow = val.shift();
          this.$refs.multipleTable.toggleRowSelection(delRow, false);
        }
        if(val.length === 0){
          this.chooseUserList = null
        }
      }
    },
    // 删除tag
    handleCloseTag(user){
      this.chooseUserList.splice(this.chooseUserList.indexOf(user), 1);
      this.$refs.multipleTable.toggleRowSelection(user, false)
    },
    // 确认
    primary(){
      if(this.chooseUserList.length>0){
        this.$emit("confirmUser",this.chooseUserList)
      }else{
        this.$modal.msgWarning("请选择人员！");
      }
    }
  }
};
</script>
<style scoped>

</style>
